package com.lottery.ruok.lottrey.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.TransferPayActivity;
import com.lottery.ruok.lottrey.adapter.BankRemittanceAdapter;
import com.lottery.ruok.lottrey.eventbusObject.ChargeSuccess;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.RechargeRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRechargeBankResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.RechargeResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

/**
 * Created by ASUS on 2017/7/27.
 *
 * 银行卡转账fragment
 */

public class BankRemittanceFragment extends BaseFragment implements TextWatcher {


    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.money)
    EditText money;
    @BindView(R.id.time)
    EditText time;
    @BindView(R.id.submit)
    TextView submit;

    private BankRemittanceAdapter myAdapter;
    private String bank_id;
    private TransferPayActivity transferPayActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.transfer_bank_lottery_fragment, container, false);

        ButterKnife.bind(this, view);

        init();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        transferPayActivity = (TransferPayActivity)activity;
    }

    private void init() {
        time.setText(Utils.toYMD(System.currentTimeMillis()));

        money.setText(transferPayActivity.getIntent().getStringExtra("money"));

        name.addTextChangedListener(this);
        money.addTextChangedListener(this);
        time.addTextChangedListener(this);

        myAdapter = new BankRemittanceAdapter(transferPayActivity);
//        listview.setAdapter(myAdapter);
//
//        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                for(GetRechargeBankResponse.DataBean getRechargeBankResponse : transferPayActivity.getRechargeBankResponses.get(transferPayActivity.bankTransferIndex).getData()){
//                    getRechargeBankResponse.setChoose(false);
//                }
//
//                transferPayActivity.getRechargeBankResponses.get(transferPayActivity.bankTransferIndex).getData().get(i).setChoose(true);
//                bank_id = transferPayActivity.getRechargeBankResponses.get(transferPayActivity.bankTransferIndex).getData().get(i).getId();
//
//                myAdapter.notifyDataSetChanged();
//
//                changeSubmitButton();
//            }
//        });
    }

    private void changeSubmitButton(){
        boolean hasChoose = false;
        if(transferPayActivity != null && transferPayActivity.getRechargeBankResponses != null) {
            if (transferPayActivity.getRechargeBankResponses.get(transferPayActivity.bankTransferIndex).getData() != null) {
                for (GetRechargeBankResponse.DataBean getRechargeBankResponse : transferPayActivity.getRechargeBankResponses.get(transferPayActivity.bankTransferIndex).getData()) {
                    if (getRechargeBankResponse.isChoose()) {
                        hasChoose = true;
                        break;
                    }
                }
            }

            if (hasChoose && name.getText().length() > 0 && money.getText().length() > 0 && time.getText().length() > 0) {
                submit.setEnabled(true);
                submit.setTextColor(Color.parseColor("#ffffff"));
            } else {
                submit.setEnabled(false);
                submit.setTextColor(Color.parseColor("#9942F7"));
            }
        }
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        changeSubmitButton();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        //sendAnalyticMsg("lottery_" + ChargeActivity.ANALYTIC_VERSION + "_me_recharge_manual_bank_submit");

        RetrofitBase.getRetrofit().create(RechargeRequest.class).getResult(UserInfo.getToken(), money.getText().toString(), bank_id, "0", null, "暂无", "暂无", name.getText().toString(), time.getText().toString())
                .compose(RxHelper.<RechargeResponse>handleResult())
                .subscribe(new RxSubscribe<RechargeResponse>(getActivity()) {
                    @Override
                    protected void _onNext(RechargeResponse rechargeResponse) {
                        EventBus.getDefault().post(new ChargeSuccess());

//                        Utils.showToast("如有充值请点击刷新余额");
                        Utils.showToast("汇款信息提交成功");
                        getActivity().finish();
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }
}
